package com.seek.quartz;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.seek.service.SysmsgService;
import com.seek.util.CommonUtil;
import com.seek.util.DateUtil;
import com.seek.util.PushtoSingle;

/**
 * app消息推送自动任务
 * @author lushuangteng
 *
 */
public class AppMsgSendQuartz {
	private static Logger logger = LoggerFactory.getLogger(OperStatsCountQuartz.class);
	@Resource
	private SysmsgService sysmsgService;

	@SuppressWarnings("unchecked")
	public void run(){
		try{
			// 检索到发送时间已到但尚未发送的app推送消息
			String sql = "select id,title,content from t_sysmsg where is_used=1 and type=1 and now()>need_send_time and status=0 and need_send_time>date_sub(curdate(),interval 2 day)";
			List<Map<String,Object>> data = sysmsgService.findMapBySql(sql);
			if (data == null || data.size() < 1)
				return;
			for (Map<String,Object> m : data){
				String title = m.get("title") == null ? null : m.get("title").toString();
				String content = m.get("content") == null ? null : m.get("content").toString();
				if (CommonUtil.isEmpty(title) || CommonUtil.isEmpty(content)){
					continue;
				}
				String payLoad = "{'mTitle':'"+title+"','logo':'http://huajinbao.b0.upaiyun.com/logo/m_all.png','content':'"+content+"','type':'1','createTime':'"+DateUtil.dateFormat(new Date(), DateUtil.DATE_FORMAT_YMDHMS)+"'}";
				boolean b = PushtoSingle.pushMessageToApp(content, payLoad);
				if (b)
					sysmsgService.updateMsgSendStatus(m.get("id") == null ? null : m.get("id").toString());
			}
		}catch(Exception e){
			logger.error(e.getMessage());
		}
	}
}
